#!/usr/bin/env python
'''View archive content'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"
__license__ = "BSD" # see LICENSE



def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv

    from optparse import OptionParser
    from os.path import isfile, splitext
    from os import system

    p = OptionParser("usage: %prog FILENAME")
    opts, args = p.parse_args()

    if len(args) != 1:
        p.error("wrong number of arguments") # Will exit

    infile = args[0]
    if not isfile(infile):
        raise SystemExit("%s - no such file" % infile)

    ext2cmd = {
        ".zip" : "unzip -l",
        ".jar" : "unzip -l", # JAR is just a zip file
        ".egg" : "unzip -l", # Egg is just a zip file
        ".tar.bz2" : "tar -tjf",
        ".tar.gz" : "tar -tzf",
        ".tgz" : "tar -tzf",
        ".tar" : "tar -tf",
        ".7z" : "7za l",
    }

    linfile = infile.lower()
    for ext in ext2cmd.iterkeys():
        if linfile.endswith(ext):
            cmd = ext2cmd[ext]
            break
    else: # Not found
        raise SystemExit("don't know how to handle %s" % infile)

    raise SystemExit(system("%s %s" % (cmd, infile)))

if __name__ == "__main__":
    main()
